
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html id="designdetector-com" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/css/admin.css" rel="stylesheet" type="text/css"></link>
<script type="text/javascript" src="/js/jquery.js"> </script>
<title>admin page</title>
</head>
<body>
  <div class="main">
    id <input id="new-id" type="text" value="" size="10">
    name <input id="new-name" type="text" value="" size="10">
    title <input id="new-title" type="text" value="" size="40">
    <a id="new-category" href="#">新建类别</a>
    <ul>
    <?php 
    $conn = mysql_connect("127.0.0.1","root","");
    mysql_set_charset('utf8',$conn);
    mysql_select_db("xingtan");

    $sql=sprintf("SELECT id, name, title FROM categories");
    $result = mysql_query($sql);

    while($row = mysql_fetch_array($result)) {
      printf('<li>id <span id="category-id">0x%x</span>', $row['id']);
      print('&nbsp;name <input id="category-name" type="text" size="10" value="' . $row['name'] . '">' 
        . ' title <input id="category-title" type="text" size="40" value="'. $row['title']. '"/>' 
        . ' <span><a href="#nogo" class="update-button" id="' . $row['id'] . '">update</a> <a href="#nogo" class="del-button" id="' . $row['id'] . '">delete</a></span></li>');
    }
    mysql_close($conn);
    ?>
    </ul>
	</div>
<script type="text/javascript">
    $('.del-button').click(
      function() {
        // alert($(this).attr('id'));
        $.ajax({
          url: "/category-del.php?id=" + $(this).attr('id'),
          context : $(this).parent(),
          success : function() {
            $(this).text("删除成功");
          }
        });
      }
    );
    
    $('.update-button').click(
      function() {
        var li = $(this).parent().parent();
        $.ajax({
          url: "/category-update.php?id=" + $('#category-id', li).text()
            + '&n=' + encodeURIComponent($('#category-name', li).val())
            + '&t=' + encodeURIComponent($('#category-title', li).val()),
          context : $(this),
          success : function(rsp_text) {
            alert(rsp_text);
          }, 
          error : function() {
            alert('更新失败');
          }
        });
      }
    );

    $('#new-category').click(
      function() {
        var id = $('#new-id').val();
        var name = $('#new-name').val();
        var title = $('#new-title').val();
        if(id.length<=0 || name.length <= 0 || title.length <=0) {
          alert('字段不全');
          return;
        }
        $.ajax({
          url: "/category-add.php?n=" + encodeURIComponent(name)
            + '&t=' + encodeURIComponent(title) 
            + '&id=' + id,
          context : $(this),
          success : function() {
             $(this).html("添加成功，请刷新页面");
          }, 
          error : function() {
            alert('添加失败');
          }
        });
      }
    );
</script>
</body>
